from django.db.models import get_apps, get_models
import datetime

def log(message):
    try:
        log = open("log.txt",'a')
        log.write("[%s] %s\n" % (datetime.datetime.now(),message))
    except IOError, e:
        raise Exception(str(e))
    finally:
        if log:
            log.close()

#log("modelhistory imported")


# Change types for history enabled models
CHANGE_TYPES = (
    ('A', 'Added'),
    ('U', 'Updated'),
    ('D', 'Deleted'),
    ('R', 'Reverted'),
)

# A cache of all history enabled models
history_model_cache = list()

# Loop through all installed apps/models and add to cache if they are history-enabled
for app in get_apps():
    for model_class in get_models(app):
        try:
            if getattr(model_class,'History'):
                if not model_class in history_model_cache:
                    history_model_cache.append(model_class)
        except AttributeError:
            continue